package leetcode101.ch3;

//LC 159
public class LengthOfLongestSubstringTwoDistinct3 {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s.length() <= 2) {
            return s.length();
        }
        int[] map = new int[128];
        int count = 0;
        int l = 0, r= 0;
        for (; r < s.length();) {
            if (map[s.charAt(r++)]++ == 0) {
                count++;
            }
            if (count > 2) {
                if (--map[s.charAt(l++)] == 0) {
                    count--;
                }
            }
        }
        return r - l;
    }

    public static void main(String[] args) {
        int l = new LengthOfLongestSubstringTwoDistinct3().lengthOfLongestSubstringTwoDistinct("aaabcd");
        System.out.println(l);
    }


}
